<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.config.TekunaConfigurationElement');
	Tekuna :: load('org.tekuna.core.config.ApplicationComponent');
	Tekuna :: load('org.tekuna.core.config.InvalidConfigurationException');


	/**
	 * Representation class that holds the values of a configured Tekuna module.
	 * Each module has a base URL, a layout (basically a template name), optionally
	 * a status and several components.
	 */

	class ApplicationModule implements TekunaConfigurationElement {

		protected
			$sBaseURL = '/',
			$sLayout = '',
			$iStatus = NULL,
			$arrComponents = array();


		/**
		 * Checks if this instance of a component is validly configured.
		 *
		 * @return boolean TRUE if valid
		 */

		public function isValid() {

			return (trim($this -> sBaseURL) != '') && (trim($this -> sLayout) != '');
		}


		/**
		 * Set the base URL
		 *
		 * @param string $sBaseURL
		 */

		public function setBaseURL($sBaseURL) {

			$this -> sBaseURL = $sBaseURL;
		}


		/**
		 * @return string returns the base URL
		 */

		public function getBaseURL() {

			return $this -> sBaseURL;
		}


		/**
		 * Set the layout file name
		 *
		 * @param string $sLayout
		 */

		public function setLayout($sLayout) {

			$this -> sLayout = $sLayout;
		}


		/**
		 * @return string returns the layout file name
		 */

		public function getLayout() {

			return $this -> sLayout;
		}


		/**
		 * Set a HTTP status
		 *
		 * @param integer $iStatus
		 */

		public function setStatus($iStatus) {

			$this -> iStatus = (integer) $iStatus;
		}


		/**
		 * @return integer the current status
		 */

		public function getStatus() {

			return $this -> iStatus;
		}


		/**
		 * Checks if a special status is set
		 *
		 * @return boolean status set or not
		 */

		public function hasStatus() {

			return ($this -> iStatus !== NULL);
		}


		/**
		 * Add a component to this module. If the component is not correct configured,
		 * a InvalidConfigurationException is thrown.
		 *
		 * @param ApplicationComponent $objComponent the new component object
		 */

		public function addComponent(ApplicationComponent $objComponent) {

			if (! $objComponent -> isValid()) {

				throw new InvalidConfigurationException("The given component is not correctly configured.");
			}

			$this -> arrComponents[] = $objComponent;
		}


		/**
		 * Retrieve all configured components for this module.
		 *
		 * @return array returns an array of ApplicationComponent objects
		 */

		public function getAllComponents() {

			return $this -> arrComponents;
		}
	}
